﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.WP.DocumentCenter.Contracts.Search;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.WP.DocumentCenter.SimpleSearch
{
    [ToolboxItemAttribute(false)]
    public class SimpleSearch : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/MS.InformationManagement.WP.DocumentCenter/SimpleSearch/SimpleSearchUserControl.ascx";
        ISearchFilterProvider _provider = null;
        SimpleSearchUserControl _SimpleSearchUserControl = null;

        protected override void CreateChildControls()
        {
            _SimpleSearchUserControl = Page.LoadControl(_ascxPath) as SimpleSearchUserControl;
            Controls.Add(_SimpleSearchUserControl);
            if (_provider != null)
            {

            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            base.OnPreRender(e);
            if (_provider != null && _provider.SearchConfig != null)
            {
                _SimpleSearchUserControl.ExecuteSearchFromHistory(_provider.SearchConfig);
            }
        }

        [ConnectionConsumer("Selected Filter")]
        public void ThisNameDoesNotMatter(ISearchFilterProvider providerInterface)
        {
            _provider = providerInterface;
        }
    }
}
